import random
import string
from datetime import datetime, timedelta
from time import sleep
from dao.mqtthelp_v2 import MQTTClient
from utils.config import Config

# 生成南线每个雷达设备的车辆数据  up
def creatSouthData(path_nanxian):
    south_vehicle_data = []
    current_time = datetime.now()
    for i in range(len(path_nanxian)):
        device_data = {
            "location":path_nanxian[i][0],
            "device_num":path_nanxian[i][3],
            "timestamp":current_time.strftime("%Y/%m/%d-%H:%M:%S"),
            "longitude":path_nanxian[i][1],
            "latitude":path_nanxian[i][2],
            "altitude":45.00,
            "number":3,
            "traffic_status_list":
                [
                    {
                        "lane_num":1,
                        "lane_dir":1,
                        "km_dir":1,
                        "speed_section":60.2,
                        "speed_lane":round(random.uniform(0,60), 2),
                        "headway":260,
                        "space_occupancy":30,
                        "jam_level":random.randint(0, 1),
                        "alarm":0,
                        "traffic_efficiency":100,
                        "time_headway":5
                    },
                    {
                        "lane_num":2,
                        "lane_dir":1,
                        "km_dir":2,
                        "speed_section":60.2,
                        "speed_lane":round(random.uniform(0,60), 2),
                        "headway":260,
                        "space_occupancy":30,
                        "jam_level":0,#random.randint(1, 2),
                        "alarm":0,
                        "traffic_efficiency":100,
                        "time_headway":5
                    },
                    {
                        "lane_num":3,
                        "lane_dir":1,
                        "km_dir":2,
                        "speed_section":60.2,
                        "speed_lane":round(random.uniform(0,60), 2),
                        "headway":260,
                        "space_occupancy":30,
                        "jam_level":1,#random.randint(0, 2),
                        "alarm":1,
                        "traffic_efficiency":100,
                        "time_headway":5
                    }
                ]
        }

        south_vehicle_data.append(device_data)
    return south_vehicle_data

# 生成北线每个雷达设备的车辆数据 down
def creatNorthData(path_beixian):
    north_vehicle_data = []
    current_time = datetime.now()
    for i in range(len(path_beixian)):
        device_data = {
            "location":path_beixian[i][0],
            "device_num":path_beixian[i][3],
            "timestamp":current_time.strftime("%Y/%m/%d-%H:%M:%S"),
            "longitude":path_beixian[i][1],
            "latitude":path_beixian[i][2],
            "altitude":45.00,
            "number":3,
            "traffic_status_list":
                [
                    {
                        "lane_num":4,
                        "lane_dir":1,
                        "km_dir":1,
                        "speed_section":60.2,
                        "speed_lane":round(random.uniform(0,60), 2),
                        "headway":260,
                        "space_occupancy":30,
                        "jam_level":random.randint(2, 4),
                        "alarm":0,
                        "traffic_efficiency":100,
                        "time_headway":5
                    },
                    {
                        "lane_num":5,
                        "lane_dir":1,
                        "km_dir":2,
                        "speed_section":60.2,
                        "speed_lane":round(random.uniform(0,60), 2),
                        "headway":260,
                        "space_occupancy":30,
                        "jam_level":random.randint(3, 4),
                        "alarm":0,
                        "traffic_efficiency":100,
                        "time_headway":5
                    },
                    {
                        "lane_num":6,
                        "lane_dir":1,
                        "km_dir":2,
                        "speed_section":60.2,
                        "speed_lane":round(random.uniform(0,60), 2),
                        "headway":260,
                        "space_occupancy":30,
                        "jam_level":4,#random.randint(0, 4),
                        "alarm":0,
                        "traffic_efficiency":100,
                        "time_headway":5
                    }
                ]
        }

        north_vehicle_data.append(device_data)
    return north_vehicle_data

# 生成闸道每个雷达设备的车辆数据
def creatZhadaoData(path_zhadao):
    zhadao_vehicle_data = []
    current_time = datetime.now()
    for i in range(len(path_zhadao)):
        device_data = {
            "location":path_zhadao[i][0],
            "device_num":path_zhadao[i][3],
            "timestamp":current_time.strftime("%Y/%m/%d-%H:%M:%S"),
            "longitude":path_zhadao[i][1],
            "latitude":path_zhadao[i][2],
            "altitude":45.00,
            "number":3,
            "traffic_status_list":
                [
                    {
                        "lane_num":random.randint(7, 11),
                        "lane_dir":1,
                        "km_dir":1,
                        "speed_section":60.2,
                        "speed_lane":round(random.uniform(0,60), 2),
                        "headway":260,
                        "space_occupancy":30,
                        "jam_level":1,#random.randint(0, 4),
                        "alarm":0,
                        "traffic_efficiency":100,
                        "time_headway":5
                    },
                    {
                        "lane_num":random.randint(7, 11),
                        "lane_dir":1,
                        "km_dir":2,
                        "speed_section":60.2,
                        "speed_lane":round(random.uniform(0,60), 2),
                        "headway":260,
                        "space_occupancy":30,
                        "jam_level":2,#random.randint(0, 4),
                        "alarm":0,
                        "traffic_efficiency":100,
                        "time_headway":5
                    },
                    {
                        "lane_num":random.randint(7, 11),
                        "lane_dir":1,
                        "km_dir":2,
                        "speed_section":60.2,
                        "speed_lane":round(random.uniform(0,60), 2),
                        "headway":260,
                        "space_occupancy":30,
                        "jam_level":3,#random.randint(0, 4),
                        "alarm":1,
                        "traffic_efficiency":100,
                        "time_headway":5
                    }
                ]
        }

        zhadao_vehicle_data.append(device_data)
    return zhadao_vehicle_data

# 实例化 MQTTClient
mqtt_client = MQTTClient()

config = Config('./conf/config.ini')
nanxian_main=config.read_value('leishi', 'nanxian_main')
nanxian_zhuru =config.read_value('leishi', 'nanxian_zhuru')
nanxian_zharu =config.read_value('leishi', 'nanxian_zharu')
nanxian_zhuchu=config.read_value('leishi', 'nanxian_zhuchu')
nanxian_zhachu=config.read_value('leishi', 'nanxian_zhachu')

beixian_main = config.read_value('leishi', 'beixian_main')
beixian_zhuru=config.read_value('leishi', 'beixian_zhuru')
beixian_zharu=config.read_value('leishi', 'beixian_zharu')
beixian_zhuchu=config.read_value('leishi', 'beixian_zhuchu')
beixian_zhachu=config.read_value('leishi', 'beixian_zhachu')

nanxian= nanxian_zhuru + ',' + nanxian_main + ',' + nanxian_zhuchu
beixian= beixian_zhuru + ',' + beixian_main + ',' + beixian_zhuchu
zhadao=nanxian_zhachu+',' + nanxian_zharu+ ',' + beixian_zhachu+',' + beixian_zharu
path_nanxian=eval(nanxian)
path_beixian=eval(beixian)
path_zhadao=eval(zhadao)

for i in range(200000000):
    # 输出所有雷达设备的数据
    all_vehicle_data=creatSouthData(path_nanxian)+creatNorthData(path_beixian)+creatZhadaoData(path_zhadao)
    print(all_vehicle_data)
    mqtt_client.concurrent_publish("bizdata/traffic_status_list", all_vehicle_data)
    sleep(1)
    i+=1
